package com.jiezheng.huinong.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.jiezheng.huinong.db.conf.IIndexManageDAO;
import com.jiezheng.huinong.model.IndexInfo;
import com.jiezheng.huinong.model.PayIndexInfo;
import com.jiezheng.huinong.util.DbUtil;

public class IndexManageDAOImpl implements IIndexManageDAO{

	/**
	 * 增加指标
	 */
	@Override
	public void addIndex(IndexInfo index) {
		
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.indexManage.addIndex", index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	
	/**
	 * 删除指标
	 */
	@Override
	public void deleteIndex(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.indexManage.deleteIndex", index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}	
	}

	/**
	 * 修改指标
	 */
	@Override
	public void modifyIndex(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.indexManage.modifyIndex",
					index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	
	/**
	 * 获取所有的指标数
	 */
	@Override
	public Integer getTotalIndexNum( IndexInfo cond) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.indexManage.getTotalIndexNum", cond);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	/**
	 * 获取单页的指标数
	 */
	@Override
	public List<IndexInfo> getIndexInfo(IndexInfo cond, Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<IndexInfo> indexs = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("indexNo", cond.getIndexNo());
		rowInfo.put("budgetUnit", cond.getBudgetUnit());
		rowInfo.put("indexSource", cond.getIndexSource());
		rowInfo.put("indexType", cond.getIndexType());
		rowInfo.put("fundProp", cond.getFundProp());
		rowInfo.put("payMethod", cond.getPayMethod());
		rowInfo.put("budgetItem", cond.getBudgetItem());
		rowInfo.put("funcType", cond.getFuncType());
		rowInfo.put("financialType", cond.getFinancialType());
		rowInfo.put("settleMethod", cond.getSettleMethod());
		rowInfo.put("budgetType", cond.getBudgetType());
		rowInfo.put("paperDate", cond.getPaperDate());
		rowInfo.put("status", cond.getStatus());
		rowInfo.put("deptNo", cond.getDeptNo());
		
		try {
			indexs = session.selectList(
					"com.jiezheng.huinong.indexManage.getIndexInfoByPage", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}
	
	/**
	 * 根据指标关键值获取相关的指标信息
	 */
	@Override
	public IndexInfo getIndexInfo(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		IndexInfo idx = null;
		
		try {
			idx = session.selectOne(
					"com.jiezheng.huinong.indexManage.getIndexInfoByNo", index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return idx;
	}
	
	/**
	 * 根据指标关键值获取相关的打印支付信息
	 */
	@Override
	public PayIndexInfo getPayIndexInfo(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		PayIndexInfo idxPay = null;
		
		try {
			idxPay = session.selectOne(
					"com.jiezheng.huinong.indexManage.getPayIndexInfoByNo", index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return idxPay;
	}
	
	/**
	 * 根据指标关键值获取相关的指标详细信息
	 */
	@Override
	public IndexInfo getDetailIndexInfo(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		IndexInfo idx = null;
		
		try {
			idx = session.selectOne(
					"com.jiezheng.huinong.indexManage.getDetailIndexInfo", index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return idx;
	}

	@Override
	public void changeStatus(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		
		try {
			session.update("com.jiezheng.huinong.indexManage.changeStatus",
					index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public Integer getTotalJointIndexNum(IndexInfo cond) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.indexManage.getTotalJointIndexNum", cond);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<IndexInfo> getJointIndexInfo(IndexInfo cond, Integer startRow,
			Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<IndexInfo> indexs = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("budgetUnit", cond.getBudgetUnit());
		rowInfo.put("budgetItem", cond.getBudgetItem());
		
		try {
			indexs = session.selectList(
					"com.jiezheng.huinong.indexManage.getJointIndexInfo", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}

	@Override
	public Integer getTotalToAuditIndexNum(IndexInfo cond) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.indexManage.getTotalToAuditIndexNum", cond);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public List<IndexInfo> getToAuditIndexInfo(IndexInfo cond,
			Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<IndexInfo> indexs = null;
		
		//多值参数据的传递，包括起始行，与行数，实现分页
		Map<String,Object> rowInfo = new HashMap<String, Object>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);
		rowInfo.put("indexNo", cond.getIndexNo());
		rowInfo.put("budgetUnit", cond.getBudgetUnit());
		rowInfo.put("indexSource", cond.getIndexSource());
		rowInfo.put("indexType", cond.getIndexType());
		rowInfo.put("fundProp", cond.getFundProp());
		rowInfo.put("payMethod", cond.getPayMethod());
		rowInfo.put("budgetItem", cond.getBudgetItem());
		rowInfo.put("funcType", cond.getFuncType());
		rowInfo.put("financialType", cond.getFinancialType());
		rowInfo.put("settleMethod", cond.getSettleMethod());
		rowInfo.put("budgetType", cond.getBudgetType());
		rowInfo.put("paperDate", cond.getPaperDate());
		rowInfo.put("status", cond.getStatus());
		rowInfo.put("deptNo", cond.getDeptNo());
		
		try {
			indexs = session.selectList(
					"com.jiezheng.huinong.indexManage.getToAuditIndexInfoByPage", rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}

	@Override
	public void reduceSpareMoney(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		
		try {
			session.update("com.jiezheng.huinong.indexManage.reduceSpareMoney",
					index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
		
	}

	@Override
	public void increaseSpareMoney(IndexInfo index) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		
		try {
			session.update("com.jiezheng.huinong.indexManage.increaseSpareMoney",
					index);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}
	
	@Override
	public List<IndexInfo> getStatisticIndexInfo() {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<IndexInfo> indexs = null;
		
		try {
			indexs = session.selectList(
					"com.jiezheng.huinong.indexManage.getStatisticIndexInfo");
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return indexs;
	}
}
